#include <stdio.h>
#include <assert.h>

#define STM32F1xx
//#define STM32F7xx
#define USE_UART1
//#define USE_UART3

#if defined(STM32F1xx)
#include <stm32f1xx_hal.h>
#elif defined(STM32F7xx)
#include <stm32f7xx_hal.h>
#endif

#ifdef USE_UART1
extern UART_HandleTypeDef huart1;
#define UART_TXE __HAL_UART_GET_FLAG(&huart1, UART_FLAG_TXE) //查询发送寄存器为空，1：可以装载新发送数据
#if defined(STM32F1xx)
#define UART_DR huart1.Instance->DR                        //串口发送寄存器, stm32f1系类
#elif defined(STM32F7xx)
#define UART_DR huart1.Instance->TDR                        //串口发送寄存器, stm32f7系类
#endif
#endif

#ifdef USE_UART3
extern UART_HandleTypeDef huart3;
#define UART_TXE __HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE) //查询发送寄存器为空，1：可以装载新发送数据
#if defined(STM32F1xx)
#define UART_DR huart3.Instance->DR                        //串口发送寄存器, stm32f1系类
#elif defined(STM32F7xx)
#define UART_DR huart3.Instance->TDR                        //串口发送寄存器, stm32f7系类
#endif
#endif

#if defined(__ARMCC_VERSION) //识别AC5、AC6 AC6使用了GUNC标志
int fputc(int ch, FILE *stream)
{
    
    while (!UART_TXE)
        ;
    UART_DR = (uint8_t)ch;
    return ch;
}

int stderr_putchar(int ch) //参考CMSIS retarget_io.c文件
{
    while (!UART_TXE)
        ;
    UART_DR = (uint8_t)ch;
    return ch;
}
/*
*   需要输出断言信息应当设置优化等级0
*/
void __aeabi_assert(const char *expr, const char *file, int line) //参考CMSIS retarget_io.c文件
{
    char str[12], *p;

    fputs("*** assertion failed: ", stderr);
    fputs(expr, stderr);
    fputs(", file ", stderr);
    fputs(file, stderr);
    fputs(", line ", stderr);

    p = str + sizeof(str);
    *--p = '\0';
    *--p = '\n';
    while (line > 0)
    {
        *--p = '0' + (line % 10);
        line /= 10;
    }
    fputs(p, stderr);

    abort();
}

void abort(void)
{
    for (;;)
        ;
}
#elif defined(__GUNC__)
int __io_putchar(int ch)
{
}

int _write(int file, char *ptr, int len)
{

    int DataIdx;

    for (DataIdx = 0; DataIdx < len; DataIdx++)

    {

        __io_putchar(*ptr++);
    }

    return len;
}
#endif
